package io.renren.modules.eightapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.eightapp.dao.EiBuildingInfoDao;
import io.renren.modules.eightapp.dao.EiHouseInfoDao;
import io.renren.modules.eightapp.dao.EiOwnerInfoDao;
import io.renren.modules.eightapp.dao.EiVillageInfoDao;
import io.renren.modules.eightapp.dto.*;
import io.renren.modules.eightapp.entity.EiBuildingInfoEntity;
import io.renren.modules.eightapp.entity.EiHouseInfoEntity;
import io.renren.modules.eightapp.entity.EiOwnerInfoEntity;
import io.renren.modules.eightapp.entity.EiVillageInfoEntity;
import io.renren.modules.eightapp.service.EiOwnerInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2023-01-03
 */
@Service
public class EiOwnerInfoServiceImpl extends CrudServiceImpl<EiOwnerInfoDao, EiOwnerInfoEntity, EiOwnerInfoDTO> implements EiOwnerInfoService {

    @Autowired
    private EiOwnerInfoDao eiOwnerInfoDao;
    @Autowired
    private EiVillageInfoDao eiVillageInfoDao;
    @Autowired
    private EiBuildingInfoDao eiBuildingInfoDao;
    @Autowired
    private EiHouseInfoDao eiHouseInfoDao;

    @Override
    public QueryWrapper<EiOwnerInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<EiOwnerInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Integer getOwnerIdByCard(Integer idcard) {
        QueryWrapper<EiOwnerInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select().eq(EiOwnerInfoEntity::getIdcard, idcard);
        List<EiOwnerInfoEntity> ownerInfoEntities = eiOwnerInfoDao.selectList(queryWrapper);
        return ownerInfoEntities.get(0).getId();
    }

    @Override
    public Integer getVillageIdByOwnerId(Integer id) {
        EiOwnerInfoEntity eiOwnerInfoEntity = eiOwnerInfoDao.selectById(id);
        Integer villageId = eiOwnerInfoEntity.getVillageId();
        return villageId;
    }

    @Override
    public CascadeDTO getCascadeList() {
        //创建级联总体
        CascadeDTO cascadeDTO = new CascadeDTO();

        //创建一级菜单集合
        List<CascadeFirstDTO> cascadeFirstDTOList = new ArrayList<>();

        //查询所有小区
        QueryWrapper<EiVillageInfoEntity> eiVillageInfoQueryWrapper = new QueryWrapper<>();
        eiVillageInfoQueryWrapper.lambda().select();
        List<EiVillageInfoEntity> villageInfoEntityList = eiVillageInfoDao.selectList(eiVillageInfoQueryWrapper);

        //查询所有楼房
        QueryWrapper<EiBuildingInfoEntity> buildingInfoQueryWrapper = new QueryWrapper<>();
        buildingInfoQueryWrapper.lambda().select();
        List<EiBuildingInfoEntity> eiBuildingInfoEntities = eiBuildingInfoDao.selectList(buildingInfoQueryWrapper);

        //查询所有房屋
        QueryWrapper<EiHouseInfoEntity> houseInfoQueryWrapper = new QueryWrapper<>();
        houseInfoQueryWrapper.lambda().select();
        List<EiHouseInfoEntity> eiHouseInfoEntities = eiHouseInfoDao.selectList(houseInfoQueryWrapper);

        //遍历所有小区
        for (EiVillageInfoEntity village :
                villageInfoEntityList) {
            //将所有小区赋值给一级菜单
            //创建一级菜单
            CascadeFirstDTO cascadeFirstDTO = new CascadeFirstDTO();
            cascadeFirstDTO.setValue(village.getId());
            cascadeFirstDTO.setLabel(village.getName());

            //创建二级菜单集合
            List<CascadeSecondDTO> cascadeSecondDTOList = new ArrayList<>();

            for (EiBuildingInfoEntity building :
                    eiBuildingInfoEntities) {
                if (building.getVillageId() == village.getId()) {
                    //创建二级菜单
                    CascadeSecondDTO cascadeSecondDTO = new CascadeSecondDTO();
                    cascadeSecondDTO.setValue(building.getId());
                    cascadeSecondDTO.setLabel(building.getBuildingName());

                    //创建三级菜单集合
                    List<CascadeThirdDTO> cascadeThirdDTOList = new ArrayList<>();

                    for (EiHouseInfoEntity house :
                            eiHouseInfoEntities) {
                        if (house.getBuildingId() == building.getId()) {
                            //创建三级菜单
                            CascadeThirdDTO cascadeThirdDTO = new CascadeThirdDTO();
                            cascadeThirdDTO.setValue(house.getId());
                            cascadeThirdDTO.setLabel(house.getUnit());
                            cascadeThirdDTOList.add(cascadeThirdDTO);
                        }
                    }
                    cascadeSecondDTO.setChildren(cascadeThirdDTOList);
                    cascadeSecondDTOList.add(cascadeSecondDTO);
                }
            }
            //对第一菜单中的属性第二菜单赋值
            cascadeFirstDTO.setChildren(cascadeSecondDTOList);
            cascadeFirstDTOList.add(cascadeFirstDTO);
        }
        cascadeDTO.setCascadeFirstDTOList(cascadeFirstDTOList);
        return cascadeDTO;
    }


}